# -*- coding: utf-8 -*-
'''
Created on 16.01.2012

@author: SkyNET
'''
import couchdb
import sys
args = sys.argv
input=args[1:]
couch=couchdb.Server("http://localhost:5984")
db=couch["users"]
i=str(input)
i=i.split(" ")
name=i[0]
row=[]
try:
    map_fun='''function(doc) {if(doc.'''+name+''')  emit(doc.'''+name+''');}'''
    for row in db.query(map_fun):
        result=row.key
    if(row!=[]):
        print 1 # user have been created
    else:
        try:
            doc={}
            doc["password"]=i[1]
            doc["email"]=i[2]
            doc["stack"]=0
            doc["numb_game"]=0
            doc["numb_win"]=0
            d={}
            d[i[0]]=doc
            db.save(d)
            print 0 # all ok
        except:
            print 2 # fail
except:
    print 2